home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / class / lowfile.d < prev    next >
Text File  |  1996-02-04  |  3KB  |  176 lines

  1.  
  2. /*
  3.  *
  4.  *    Copyright (c) 1993-1996 Algorithms Corporation
  5.  *    3020 Liberty Hills Drive
  6.  *    Franklin, TN  37067
  7.  *
  8.  *    ALL RIGHTS RESERVED.
  9.  *
  10.  *
  11.  *
  12.  */
  13.  
  14.  
  15.  
  16. #include <fcntl.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #ifndef    unix
  20. #include <io.h>
  21. #endif
  22.  
  23. #if    defined(sparc)  ||  defined(unix)
  24. #include <unistd.h>
  25. #define    tell(h)        lseek(h, 0L, SEEK_CUR)
  26. #endif
  27.  
  28.  
  29. defclass  LowFile : Stream  {
  30.     iName;
  31.     int    iHandle;
  32. };
  33.  
  34. /* extern    long    lseek(), tell(); */
  35.  
  36. cmeth    gNew()
  37. {
  38.     return gShouldNotImplement(self, "gNew");
  39. }
  40.  
  41. cmeth    gOpenLowFile, <vNew> (char *name, int oflag, int pmode)
  42. {
  43.     object    obj;
  44.     ivType    *iv;
  45.     int    handle;
  46.  
  47.     if (IsObj((object) name))
  48.         name = gStringValue((object) name);
  49.     handle = open(name, oflag, pmode);
  50.     if (handle == -1)
  51.         return NULL;
  52.     obj = gNew(super);
  53.     iv = ivPtr(obj);
  54.     iName = gNewWithStr(String, name);
  55.     iHandle = handle;
  56.     return obj;
  57. }
  58.  
  59.  
  60. imeth    object    gDispose, gDeepDispose, gGCDispose ()
  61. {
  62.     close(iHandle);
  63.     gDispose(iName);
  64.     return gDispose(super);
  65. }
  66.  
  67. imeth    int    gRead(char *buf, unsigned n)
  68. {
  69.     return read(iHandle, buf, n);
  70. }
  71.  
  72. imeth    int    gWrite(char *buf, unsigned n)
  73. {
  74.     return write(iHandle, buf, n);
  75. }
  76.  
  77. imeth    char    *gGets(char *buf, int sz)   /*  very slow - not for real use  */
  78. {
  79.     int    h = iHandle;
  80.     int    i, r;
  81.     char    c;
  82.  
  83.     if (sz <= 0)
  84.         return NULL;
  85.     if (sz-- == 1)  {
  86.         *buf = '\0';
  87.         return buf;
  88.     }
  89.     for (i=0 ; i < sz ; )  {
  90.         r = read(h, &c, 1);
  91.         if (!r  &&  !i  ||  r < 0)
  92.             return NULL;
  93.         if (!r)
  94.             break;
  95.         buf[i++] = c;
  96.         if (c == '\n')
  97.             break;
  98.     }
  99.     buf[i] = '\0';
  100.     return buf;
  101. }
  102.  
  103. imeth    long    gAdvance(long n)
  104. {
  105.     int    h = iHandle;
  106.     long    p = tell(h);
  107.     long    r = lseek(h, n, SEEK_CUR);
  108.     return r >= 0L ? r-p : 0L;
  109. }
  110.  
  111. imeth    long    gRetreat(long n)
  112. {
  113.     int    h = iHandle;
  114.     long    p = tell(h);
  115.     long    r = lseek(h, -n, SEEK_CUR);
  116.     return r >= 0L ? p-r : 0L;
  117. }
  118.  
  119. imeth    long    gSeek(long n)
  120. {
  121.     long    r = lseek(iHandle, n, SEEK_SET);
  122.     return r;
  123. }
  124.  
  125. imeth    long    gPosition()
  126. {
  127.     return tell(iHandle);
  128. }
  129.  
  130. imeth    long    gLength()
  131. {
  132.     struct    stat    sb;
  133.     int    r;
  134.  
  135.     r = fstat(iHandle, &sb);
  136.     return r ? -1L : sb.st_size;
  137. }
  138.  
  139. imeth    char    *gName()
  140. {
  141.     return gStringValue(iName);
  142. }
  143.  
  144. imeth    int    gEndOfStream()
  145. {
  146. #ifndef unix    /*  eof() not available on unix  */
  147.     return eof(iHandle);
  148. #else
  149.     gShouldNotImplement(self, "EndOfStream");
  150.     iHandle = iHandle;   /*  keep the compiler happy  */
  151.     return 0;  /*  never reached  */
  152. #endif
  153. }
  154.  
  155. imeth    int    gFileHandle()
  156. {
  157.     return iHandle;
  158. }
  159.  
  160.  
  161.  
  162.  
  163.  
  164. /*
  165.  *
  166.  *    Copyright (c) 1993-1996 Algorithms Corporation
  167.  *    3020 Liberty Hills Drive
  168.  *    Franklin, TN  37067
  169.  *
  170.  *    ALL RIGHTS RESERVED.
  171.  *
  172.  *
  173.  *
  174.  */
  175.  
  176.